package org.mozhu.mboot.core.data;

import org.mozhu.mboot.core.data.entity.IEntity;
import org.mozhu.mboot.core.data.filter.SearchFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

public class BaseService<T extends IEntity<ID>, ID extends Serializable> implements IBaseService<T, ID> {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected BaseRepository<T, ID> baseRepository;

	@Autowired
	public void setBaseRepository(BaseRepository<T, ID> baseRepository) {
		this.baseRepository = baseRepository;
	}

	/**
	 * 根据主键删除相应实体
	 *
	 * @param id
	 *            主键
	 */
	@Override
	public void deleteById(ID id) {
		baseRepository.deleteById(id);
	}

	/**
	 * 删除实体
	 *
	 * @param m
	 *            实体
	 */
	@Override
	public void delete(T m) {
		baseRepository.delete(m);
	}

	/**
	 * 按照主键查询
	 *
	 * @param id
	 *            主键
	 * @return 返回id对应的实体
	 */
	@Override
	public T findById(ID id) {
		return baseRepository.findById(id).orElse(null);
	}

	/**
	 * 实体是否存在
	 *
	 * @param id
	 *            主键
	 * @return 存在 返回true，否则false
	 */
	@Override
	public boolean existsById(ID id) {
		return baseRepository.existsById(id);
	}

	/**
	 * 统计实体总数
	 *
	 * @return 实体总数
	 */
	@Override
	public long count() {
		return baseRepository.count();
	}

	/**
	 * 查询所有实体
	 *
	 * @return
	 */
	@Override
	public List<T> findAll() {
		return baseRepository.findAll();
	}

	/**
	 * 按照顺序查询所有实体
	 *
	 * @param sort
	 * @return
	 */
	@Override
	public List<T> findAll(Sort sort) {
		return baseRepository.findAll(sort);
	}

	/**
	 * 分页及排序查询实体
	 *
	 * @param pageable
	 *            分页及排序数据
	 * @return
	 */
	@Override
	public Page<T> findAll(Pageable pageable) {
		return baseRepository.findAll(pageable);
	}

	@Override
	public List<T> findAllById(Iterable<ID> ids) {
		return baseRepository.findAllById(ids);
	}

	@Override
	public List<T> findAllById(ID[] ids) {
		return baseRepository.findAllById(Arrays.asList(ids));
	}

	@Override
	public List<T> saveAll(Iterable<T> entities) {
		return baseRepository.saveAll(entities);
	}

	@Override
	public void flush() {
		baseRepository.flush();
	}

	@Override
	public void deleteInBatch(Iterable<T> entities) {
		baseRepository.deleteInBatch(entities);
	}

	@Override
	public void deleteAllInBatch() {
		baseRepository.deleteAllInBatch();
	}

	@Override
	public T getOne(ID id) {
		return baseRepository.getOne(id);
	}

	@Override
	public void deleteAll(Iterable<? extends T> entities) {
		baseRepository.deleteAll(entities);
	}

	@Override
	public void deleteAll() {
		baseRepository.deleteAll();
	}

	@Override
	public T saveAndFlush(T entity) {
		return baseRepository.saveAndFlush(entity);
	}

	@Override
	public T save(T entity) {
		return baseRepository.save(entity);
	}

	@Override
	public T findOne(Specification<T> spec) {
		return baseRepository.findOne(spec).orElse(null);
	}

	@Override
	public List<T> findAll(Specification<T> spec) {
		return baseRepository.findAll(spec);
	}

	@Override
	public Page<T> findAll(Specification<T> spec, Pageable pageable) {
		return baseRepository.findAll(spec, pageable);
	}

	@Override
	public List<T> findAll(Specification<T> spec, Sort sort) {
		return baseRepository.findAll(spec, sort);
	}

	@Override
	public long count(Specification<T> spec) {
		return baseRepository.count(spec);
	}

	@Override
	public void deleteInBatch(ID[] ids) {
		baseRepository.deleteInBatch(ids);
	}

	@Override
	public Page<T> findAll(List<SearchFilter> searchFilters, Pageable pageable) {
		return baseRepository.findAll(searchFilters, pageable);
	}

	@Override
	public Page<T> findAll(SearchFilter searchFilter, Pageable pageable) {
		return baseRepository.findAll(searchFilter, pageable);
	}

	@Override
	public List<T> findAll(List<SearchFilter> searchFilters) {
		return baseRepository.findAll(searchFilters);
	}

	@Override
	public Page<T> findAll(Searchable searchable) {
		return baseRepository.findAll(searchable);
	}

	@Override
	public long count(Searchable searchable) {
		return baseRepository.count(searchable);
	}

}
